//
//  XKLAllController.m
//  GuiZiShouDuanZi
//
//  Created by xmg on 16/5/3.
//  Copyright © 2016年 xiao_kl. All rights reserved.
//

#import "XKLAllController.h"
#import <AFNetworking.h>
#import "XKLessenceCell.h"
#import <MJExtension.h>
#import "XKLEessenceItem.h"
#import <MJRefresh.h>
#import "XKLCommentController.h"

@interface XKLAllController ()

@property (nonatomic,strong)NSMutableArray *items;
/**下次记载的数据*/
@property (nonatomic,strong)NSString *maxtime;
@end

@implementation XKLAllController

static NSString * const cellID=@"essenceCell";
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpStart];
    
    
}
//设置入口
-(void)setUpStart{
    [self upDataTableView];
}
//设置tableview的相关属性
-(void)upDataTableView{
    
    self.tableView.backgroundColor = XKLGloColor;
    //注册
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([XKLessenceCell class]) bundle:nil] forCellReuseIdentifier:cellID];
    self.tableView.contentInset=UIEdgeInsetsMake(79, 0, 49, 0);
    //指示器范围
    self.tableView.scrollIndicatorInsets=self.tableView.contentInset;
    
    //下拉和上拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(fristLoadHttp)];
    //设置自动透明
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    //一开始就要刷新一下
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(continueLoadHttp)];
}
-(NSMutableDictionary *)httpDic{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    dic[@"a"]=self.type;
    dic[@"c"]=@"data";
    return dic;
}

-(void)fristLoadHttp{
    
    NSMutableDictionary *dic=[self httpDic];
    
    dic[@"maxtime"]=@"";
    dic[@"type"]=@(self.topicType);
    [[AFHTTPSessionManager manager] GET:@"http://api.budejie.com/api/api_open.php" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.items=[XKLEessenceItem mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        //记录下次刷新的地方
        self.maxtime = [responseObject[@"info"] objectForKey:@"maxtime"];
        
        //刷新表格
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //停止刷新
        [self.tableView.mj_header endRefreshing];
    }];
    
    
}

-(void)continueLoadHttp{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    dic[@"a"]=@"list";
    dic[@"c"]=@"data";
    dic[@"maxtime"]=self.maxtime;
    dic[@"type"]=@(self.topicType);
    [[AFHTTPSessionManager manager] GET:@"http://api.budejie.com/api/api_open.php" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
        [self.items addObjectsFromArray:[XKLEessenceItem mj_objectArrayWithKeyValuesArray:responseObject[@"list"]]];
        
        //记录下次刷新的地方
        self.maxtime = [responseObject[@"info"] objectForKey:@"maxtime"];
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self.tableView.mj_footer endRefreshing];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.items.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XKLessenceCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    cell.item=self.items[indexPath.row];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    XKLEessenceItem *item=self.items[indexPath.row];
    
    return item.cellHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XKLCommentController *comment = [[XKLCommentController alloc] init];
    comment.item=self.items[indexPath.row];
    [self.navigationController pushViewController:comment animated:YES];
    
}
@end
